#include "addfrienddialog.h"

AddFriendDialog::AddFriendDialog(QWidget *parent)
    : QDialog(parent)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(500,500);
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setStyleSheet("QDialog {background-color: rgb(255, 255, 255);}");

    //创建网格布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(30,30,30,30);
    layout->setSpacing(10);
    this->setLayout(layout);

    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(25);
    searchEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    searchEdit->setStyleSheet("QLineEdit {background-color:rgb(226, 226, 226); border:none; border-radius:5px; font-size:15px; padding-left:10px;}");
    searchEdit->setPlaceholderText("按手机号/用户序号查找");

    searchBtn = new QPushButton();
    searchBtn->setFixedSize(25,25);
    searchBtn->setIconSize(QSize(25,25));
    searchBtn->setIcon(QIcon(":/image/search.png"));
    searchBtn->setStyleSheet("QPushButton{border:none; background-color:rgb(226, 226, 226);} QPushButton:hover{background-color:rgb(209, 209, 209);} QPushButton:pressed {background-color:rgb(209, 209, 209);}");

    //创建滚动区域, 这里滚动区域的初始化较为复杂, 所以建议专门实现一个init函数
    layout->addWidget(searchEdit,0,0,1,8);
    layout->addWidget(searchBtn,0,8,1,1);
    initResultArea(layout);

#if test

    for(int i = 0;i < 20;i++)
    {
        UserInfo* userinfo = new UserInfo();
        userinfo->userIcon = QIcon(":/image/logo.png");
        userinfo->userId = "1234" + QString::number(i);
        userinfo->userName = "李四" +  QString::number(i);
        userinfo->userPhone = "122334455";
        userinfo->userSignature = "测试AddFriendDialog的个性签名";

        addResult(*userinfo);
    }
#endif
    /// 搜索按钮逻辑实现
    connect(searchBtn,&QPushButton::clicked,this,&AddFriendDialog::clickSearchBtn);

}
void AddFriendDialog::addResult(const UserInfo &userInfo)
{
    FriendResultItem* item = new FriendResultItem(userInfo);
    resultContainer->layout()->addWidget(item);
}

void AddFriendDialog::clear()
{
    QVBoxLayout* layout = dynamic_cast<QVBoxLayout*>(resultContainer->layout());
    for(int i = layout->count() - 1; i>=0;i--)
    {
        QWidget* w = layout->itemAt(i)->widget();
        if(w == nullptr) continue;
        layout->removeWidget(w);
        delete w;
    }
}

void AddFriendDialog::setSearchKey(const QString &searchKey)
{
    searchEdit->setText(searchKey);
}

void AddFriendDialog::clickSearchBtn()
{
    //获取输入框内容
    QString text = searchEdit->text();

    //发送网络请求, 查询好友
    model::DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter,&DataCenter::searchUserDone,this,&AddFriendDialog::clickSearchBtnDone);
    dataCenter->searchUserAsync(text);
}

void AddFriendDialog::clickSearchBtnDone()
{
    //获取dataCenter中的用户搜索结果列表
    model::DataCenter* dataCenter = DataCenter::getInstance();

    auto list = dataCenter->getSearchFriendList();

    clear();

    for(auto& f : *list)
    {
        addResult(f);
    }
}

void AddFriendDialog::initResultArea(QGridLayout *layout)
{
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0px;}");
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width: 2px; background-color: rgb(255, 255, 255); }QScrollBar::handle:vertical {background-color: rgb(205, 205, 205);}");
    //实例化resultContainer
    resultContainer = new QWidget();
    scrollArea->setWidget(resultContainer);

    //创建水平布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(10);
    vlayout->setContentsMargins(0,0,0,0);
    vlayout->setAlignment(Qt::AlignTop);
    resultContainer->setLayout(vlayout);
    layout->addWidget(scrollArea,1,0,1,9);
}
//////////////////////////////////////////////////////////////
///搜索项逻辑
/////////////////////////////////////////////////////////////
FriendResultItem::FriendResultItem(const UserInfo &userInfo) : userInfo(userInfo)
{
    //设置基本逻辑
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    //创建网格布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0,0,30,0);
    layout->setSpacing(10);
    this->setLayout(layout);

    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50,50);
    avatarBtn->setIconSize(QSize(50,50));
    avatarBtn->setIcon(userInfo.userIcon);

    QLabel* nameLabel = new QLabel();
    nameLabel->setFixedHeight(35);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    nameLabel->setStyleSheet("QLabel{font-size:16px; font-weight: 500}");
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameLabel->setText(userInfo.userName);

    QLabel* descLabel = new QLabel();
    descLabel->setFixedHeight(35);
    descLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    descLabel->setStyleSheet("QLabel{font-size:16px; font-weight: 500}");
    descLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descLabel->setText(userInfo.userSignature);

    addBtn = new QPushButton();
    addBtn->setText("添加好友");
    addBtn->setFixedSize(100,40);
    addBtn->setStyleSheet("QPushButton { border: none; background-color:rgb(137, 217, 97); color: rgb(255, 255, 255); border-radius: 10px; }QPushButton:pressed { background-color: rgb(198, 198, 198); }");

    layout->addWidget(avatarBtn,0,0,2,1);
    layout->addWidget(nameLabel,0,1,1,1);
    layout->addWidget(descLabel,1,1,1,1);
    layout->addWidget(addBtn,0,2,2,1);
    // 为添加好友按钮关联逻辑
    connect(addBtn,&QPushButton::clicked,this,&FriendResultItem::clickAddBtn);

}

void FriendResultItem::clickAddBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    addBtn->setEnabled(false);
    addBtn->setText("已申请");
    dataCenter->addFriendApplyAsync(userInfo.userId);

}




























